<template>
  <div class="password-container">
    <h2 class="title">修改密码</h2>
    <form @submit.prevent="handleSubmit" class="password-form">
      <div class="form-group">
        <label>旧密码</label>
        <input
          v-model="form.oldPassword"
          type="password"
          placeholder="请输入旧密码"
          required
        />
      </div>

      <div class="form-group">
        <label>新密码</label>
        <input
          v-model="form.newPassword"
          type="password"
          placeholder="请输入新密码（6-20位字符）"
          required
        />
      </div>

      <div class="form-group">
        <label>确认密码</label>
        <input
          v-model="form.confirmPassword"
          type="password"
          placeholder="请再次输入新密码"
          required
        />
      </div>

      <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>

      <div class="form-actions">
        <button type="submit" class="submit-btn">确认修改</button>
        <button type="button" class="btn btn-go-back" @click="goBack">返回</button>
      </div>

    </form>
  </div>
</template>

<script setup>
const router = useRouter()
const form = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const errorMessage = ref('')

const validateForm = () => {
  if (form.newPassword.length < 6 || form.newPassword.length > 20) {
    errorMessage.value = '密码长度需在6-20位之间'
    return false
  }

  if (form.newPassword !== form.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致'
    return false
  }

  errorMessage.value = ''
  return true
}

const handleSubmit = async () => {
  if (!validateForm()) return

  try {
    const { data } = await axios.put('/api/accounts/auth/user_password_update/', {
      old_password: form.oldPassword,
      password: form.newPassword
    },{ requiresAuth: true })
    alert('密码修改成功')
    router.go(-1)
  } catch (error) {
    errorMessage.value = error.message || '密码修改失败，请重试'
  }
}

const goBack = () => {
  router.go(-1)
}
</script>

<style scoped>
.password-container {
  max-width: 500px;
  margin: 55px auto;
  padding: 40px;
  background: var(--profile-bg-color,#fff);
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin: 0 0 40px;
  font-size: 24px;
  color: var(--profile-text-color,#2c3e50);
  position: relative;
  padding-bottom: 10px;
}

.title::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: var(--profile-title-color, #1e90ff);
}

.password-form {
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.form-group {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

label {
  font-weight: 600;
  color: var(--profile-label-color, #34495e);
  font-size: 15px;
}

input {
  padding: 12px 16px;
  border: 2px solid #e0e4e8;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: var(--profile-input-bg-color);
}

input:focus {
  border-color: var(--profile-input-focus-color, #1e90ff);
  box-shadow: 0 2px 8px rgba(30, 144, 255, 0.2);
}

.error-message {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #ff4757;
  font-size: 14px;
  padding: 12px;
  background: #ffecee;
  border-radius: 6px;
  margin: -10px 0;
}

.error-message::before {
  content: "⚠";
  font-size: 16px;
}
.form-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 1.5rem;
}

/* 主按钮样式 */
.submit-btn {
  background: var(--profile-btn-bg-color, linear-gradient(90deg, #409eff, #66b1ff));
  color: white;
  font-size: 1rem;
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: background 0.3s, transform 0.3s ease, box-shadow 0.3s;
}

.submit-btn:hover {
  background: var(--profile-btn-hover-bg-color, linear-gradient(90deg, #66b1ff, #409eff));
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
}

.submit-btn:disabled {
  background: #d3d3d3;
  cursor: not-allowed;
  opacity: 0.7;
}

/* 返回按钮样式 */
.btn-go-back {
  background: var(--profile-btn-bg-color, #ffffff);;
  color:var(--profile-btn-color, #409eff);
  font-size: 1rem;
  padding: 0.8rem 1.5rem;
  border: 1px solid #409eff;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.3s, color 0.3s, transform 0.3s ease, box-shadow 0.3s;
}

.btn-go-back:hover {
  background:var(--profile-btn-hover-bg-color,#409eff);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 响应式设计 */
@media (max-width: 576px) {
  .password-container {
    margin: 20px;
    padding: 25px;
  }

  .form-actions {
    flex-direction: column;
  }
}
</style>

